#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>

#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>

#define BUF_SIZE  30

void error_handling(char * buf);

int main(int argc, char * argv[])
{
    int serv_sock,clnt_sock;
    struct sockaddr_in serv_addr,clnt_addr;
    struct timeval timeout;
    fd_set reads,cpy_reads;

    socklen_t adr_sz;
    int fd_max ,str_len,fd_num,i;
    char buf[BUF_SIZE];

    if(argc !=2)
    {
        printf("Usage :%s <port>\n",argv[1]);
        exit(0);
    }
    serv_sock = socket(PF_INET,SOCK_STREAM,0);
    memset(&serv_addr,0,sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));

    if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) ==-1)
        error_handling("bing error...");
    printf("binding....\n");

    if(listen(serv_sock,5) == -1)
        error_handling("listen error...");
    printf("listening....\n");

    FD_ZERO(&reads);
    FD_SET(serv_sock,&reads);
    FD_SET(0,&reads);
    fd_max = serv_sock;
    while(1)
    {
        printf("start\n");
        cpy_reads = reads;
        timeout.tv_sec = 5;
        timeout.tv_usec = 0;

        if((fd_num = select(fd_max+1,&cpy_reads,0,0,&timeout))==-1)
        {
            printf(" select error\n");
            break;
        }
            
        if(fd_num ==0)
            continue;
        printf("accpet\n");
        for(i=0;i<fd_max+1;i++)
        {
            if(FD_ISSET(i,&cpy_reads))
            {
                if(i==0)
                {
                    str_len = read(0,buf,BUF_SIZE);
                    buf[str_len]=0;
                    printf("message from console: %s",buf);
                }
                else if(i==serv_sock)  //connect request
                {
                    adr_sz = sizeof(clnt_addr);
                    clnt_sock = accept(serv_sock,(struct sockaddr*) &clnt_addr,&adr_sz);
                    FD_SET(clnt_sock,&reads);
                    if(fd_max<clnt_sock)
                        fd_max = clnt_sock;
                    printf("connect client :%d \n",clnt_sock);
                }
                else   //read message
                {
                    str_len = read(i,buf,BUF_SIZE);
                    if(str_len == 0)
                    {
                        FD_CLR(i,&reads);
                        close(i);
                        printf("closed client :%d \n",i);
                    }
                    else
                    {
                        write(i,buf,str_len); //echo
                    }
                }
                
            }
        }
    }
    close(serv_sock);
    return 0;
    
}

void error_handling(char * message)
{
    printf("%s \n",message);
    exit(1);
}
